package wangle.com.leletexttovoice;

import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.widget.EditText;

import com.jakewharton.rxbinding.view.RxView;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import rx.functions.Action1;
import wangle.com.xunfeivoice.XunfeiTts;
import wangle.com.xunfeivoice.api.PlayVoiceApi;

/**
 * 主界面
 * Created by Administrator on 2017/2/3.
 */

public class MainActivity extends AppCompatActivity {

    private PlayVoiceApi playVoiceApi;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        playVoiceApi = getPlayVoiceApi();

        RxView.clicks(findViewById(R.id.btn_translation))
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        String text = ((EditText) findViewById(R.id.et_voice_text)).getText().toString();
                        playVoiceApi.playVoice(text);
                    }
                });

        RxView.clicks(findViewById(R.id.btn_ocr))
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @RequiresApi(api = Build.VERSION_CODES.N)
                    @Override
                    public void call(Void aVoid) {
                        try {
                            String text = OCRUtils.parseImageToString(BitmapFactory.decodeResource(MainActivity.this.getResources(), R.mipmap.test));
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });


    }

    @Override
    protected void onStop() {
        super.onStop();
        playVoiceApi.stopVoice();
    }

    @Override
    protected void onResume() {
        super.onResume();
        playVoiceApi.resumePlay();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        playVoiceApi.destory();
    }

    private PlayVoiceApi getPlayVoiceApi(){
        return new XunfeiTts(this);
    }
}
